#@edit() #define head_foot()
<link rel="stylesheet"
	href="/skin/ui/plugins/treelib/zTree_v3-3.5.28/css/zTreeStyle/zTreeStyle.css"
	type="text/css">
#end #define content()
<!-- form start -->
<form class="form-horizontal" action="/authrole/doSaveRule"
	method="post">
	<div class="box-body">
		<ul id="treeMain" class="ztree" data-url="/apis/getMenu"></ul>
	</div>
	<!-- /.box-body -->
	<div class="box-footer">
		<input type="hidden" name="id" value="#(itemData.id)" /> <input
			type="hidden" name="rules" value="#(itemData.rules)" />
		<button type="button" class="btn btn-default xsui-btn-close-dialog">取消</button>
		<button type="button" class="btn btn-primary pull-right">保存</button>
		<button type="submit" style="display: none;"></button>
	</div>
	<!-- /.box-footer -->
</form>
#end #define body_foot()
<script type="text/javascript"
	src="/skin/ui/plugins/treelib/zTree_v3-3.5.28/js/jquery.ztree.core.js"></script>
<script type="text/javascript"
	src="/skin/ui/plugins/treelib/zTree_v3-3.5.28/js/jquery.ztree.excheck.js"></script>

<SCRIPT type="text/javascript">
	$('.btn-primary').click(function() {
		var nodes = $.fn.zTree.getZTreeObj("treeMain").getCheckedNodes(true);
		var ids = '';
		for ( var i in nodes) {
			ids += ',' + nodes[i].id;
		}
		if (ids.length > 0) {
			ids = ids.substring(1);
		}
		$('input[name="rules"]').val(ids);
		$(':submit').trigger('click');
	})
	// 2017-5-17
	var setting = {
		check : {
			enable : true
		},
		data : {
			key : {
				children : "children",
				name : "title",
				title : "",
				url : "url",
			},
			simpleData : {
				enable : true,
				idKey : "id",
				pIdKey : "pid",
			}
		}
	};

	function setCheck() {
		var zTree = $.fn.zTree.getZTreeObj("treeMain"), type = {
			"Y" : "ps",
			"N" : "ps"
		};
		zTree.setting.check.chkboxType = type;
		zTree.expandAll(true);
		defaultChecked();
		setTimeout(fixHeight, 200);
	}

	$(document).ready(function() {
		$.post($("#treeMain").data('url'), function(data) {
			$.fn.zTree.init($("#treeMain"), setting, data);
			setCheck();
		})
	});
	function fixHeight() {
		parent.layer.iframeAuto(window.layerIndex);
	}
	// 初始选择
	function defaultChecked() {
		var rules = $('input[name="rules"]').val();
		if (!rules) {
			return;
		}
		var arules = rules.split(',');
		var node;
		var zTree = $.fn.zTree.getZTreeObj("treeMain")
		for ( var i in arules) {
			node = zTree.getNodeByParam("id", arules[i]);
			zTree.checkNode(node, true);
			console.log(node);
		}
	}
</SCRIPT>
#end
